var setting = {
	data: {
		simpleData: {
			enable: true,
			idKey: "id",
			pIdKey: "parentId",
			rootPId: -1
		},
		key: {
			url: "nourl"
		}
	}
};
var ztree;

var vm = new Vue({
	el: '#rrapp',
	data: {
		showList: true,
		title: null,
		classification: {},
		directionList: []
	},
	methods: {
		menuTree: function() {
			layer.open({
				type: 1,
				offset: '50px',
				skin: 'layui-layer-molv',
				title: "选择菜单",
				area: ['300px', '450px'],
				shade: 0,
				shadeClose: false,
				content: jQuery("#menuLayer"),
				btn: ['确定', '取消'],
				btn1: function(index) {
					var node = ztree.getSelectedNodes();
					//选择上级菜单
					vm.classification.parentId = node[0].id;
					vm.classification.parentName = node[0].name;

					layer.close(index);
				}
			});
		},
		getMenu: function(menuId) {
			//加载菜单树
			$.get(baseURL + "course/classification/getClassificationList", function(r) {
				ztree = $.fn.zTree.init($("#menuTree"), setting, r);
			})
		},
		query: function() {
			vm.reload();
		},
		add: function() {
			vm.showList = false;
			vm.title = "新增";
			vm.classification = {
				id:null,
				name:null,
				parentId: null,
				parentName: null
			};
			vm.getMenu()
		},
		update: function(event) {
			vm.getMenu()
			// var menu = getMenuId();
			var childrenTagName = document.getElementsByClassName("treegrid-selected")[0].className.split(" ")[1]
			var str = childrenTagName.match(/treegrid-parent-(\S*)/)[1];
			var menu = {
				id:null,
				name:null
			}
			menu.id =	document.getElementsByClassName("treegrid-"+str)[0].children[1].innerText
			menu.name = document.getElementsByClassName("treegrid-"+str)[0].children[2].innerText
			if(menu == null) {
				return;
			}
			vm.showList = false;
			vm.title = "修改";
			vm.classification.id = document.getElementsByClassName("treegrid-selected")[0].children[1].innerText
			vm.classification.name = document.getElementsByClassName("treegrid-selected")[0].children[2].innerText
			vm.classification.parentId = menu.id
			vm.classification.parentName = menu.name
		},
		getDirectionList() {
			$.ajax({
				type: "POST",
				url: baseURL + "course/direction/list",
				contentType: "application/json",
				success: function(r) {
					console.log(r)
					vm.directionList = r.page.list
				}
			});

		},

		saveOrUpdate: function(event) {

			var classificationId =  vm.classification.id
			var url = classificationId == null ? "course/classification/save" : "course/classification/update";
			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json",
				data: JSON.stringify(vm.classification),
				success: function(r) {
					if(r.code === 0) {
						alert('操作成功', function(index) {
							vm.reload();
						});
					} else {
						alert(r.msg);
					}
				}
			});
		},
		del: function(event) {
			var menuId = document.getElementsByClassName("treegrid-selected")[0].children[1].innerText
			if(menuId == null) {
				return;
			}
			confirm('确定要删除选中的记录？', function() {
				$.ajax({
					type: "GET",
					url: baseURL + "course/classification/delete?id="+menuId,
					contentType: "application/json",
					success: function(r) {
						if(r.code == 0) {
							alert('操作成功', function(index) {
								vm.reload()
							});
						} else {
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id) {
			$.get(baseURL + "course/classification/info/" + id, function(r) {
				vm.classification = r.classification;
			});
		},
		reload: function(event) {
			vm.showList = true;
			course.table.refresh();
		}
	}
});

var course = {
	id: "courseTable",
	table: null,
	layerIndex: -1
};

/**
 * 初始化表格的列
 */
course.initColumn = function() {
	var columns = [{
			field: 'selectItem',
			radio: true
		},
		{
			title: '菜单ID',
			field: 'id',
			visible: false,
			align: 'center',
			valign: 'middle',
			width: '80px'
		},
		{
			title: '类别名称',
			field: 'name',
			visible: false,
			align: 'center',
			valign: 'middle',
			width: '80px'
		},
		{
			title: '上级名称',
			field: 'parentName',
			visible: false,
			align: 'center',
			valign: 'middle',
			width: '80px'
		},

	]
	return columns;
};

function getMenuId() {
	var selected = $.map($("#memberVideo").bootstrapTable('getSelections'), function(row) {
		return row;
	});
	if(selected.length == 0) {
		alert("请选择一条记录");
		return null;
	} else {
		return selected[0];
	}
}

$(function() {
	var colunms = course.initColumn();
	var table = new TreeTable(course.id, baseURL + "course/classification/getClassificationList", colunms);
	table.setExpandColumn(2);
	table.setIdField("id");
	table.setCodeField("id");
	table.setParentCodeField("parentId");
	table.setExpandAll(false);
	table.init();
	course.table = table;
});